package com.heng.controller;

import com.heng.dao.entity.product.CategoryEntity;
import com.heng.service.CategoryService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api/categories")
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    // 新增分类
    @PostMapping
    public boolean addCategory(@RequestBody CategoryEntity categoryEntity) {
        return categoryService.save(categoryEntity);
    }

    // 删除分类（物理删除）
    @DeleteMapping("/{categoryId}")
    public boolean deleteCategory(@PathVariable Integer categoryId) {
        return categoryService.removeById(categoryId);
    }

    // 更新分类
    @PutMapping
    public boolean updateCategory(@RequestBody CategoryEntity categoryEntity) {
        return categoryService.updateById(categoryEntity);
    }

    // 按ID查询分类
    @GetMapping("/{categoryId}")
    public CategoryEntity getCategoryById(@PathVariable Integer categoryId) {
        return categoryService.getById(categoryId);
    }

    // 查询所有顶级分类
    @GetMapping("/top")
    public List<CategoryEntity> getTopCategory() {
        return categoryService.getTopCategory();
    }

    // 按父分类ID查询子分类
    @GetMapping("/parent/{parentId}")
    public List<CategoryEntity> getByParentId(@PathVariable Integer parentId) {
        return categoryService.getByParentId(parentId);
    }
}